package leetcode.simple.q283;

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        int[] nums = {0,1,0,3,12};
        int[] nums2 = {1,0,1};
        moveZeroes2(nums2);
    }


    /**
     * 看到别人的题解
     * @param nums
     */
    public static void moveZeroes(int[] nums) {
        //[0,1,0,3,12]
        int j = 0;
        //找出不为0的元素，覆盖到数组前面
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] != 0){
                nums[j++] = nums[i];
            }
        }

        //将排完之后的后面的元素覆盖为0
        for (; j < nums.length; j++) {
            nums[j] = 0;
        }
        System.out.println(Arrays.toString(nums));
        
    }

    /**
     * 看到别人的题解2
     * 交换法
     * @param nums
     */
    public static void moveZeroes2(int[] nums) {
        //[0,1,0,3,12]
        int temp = 0;
        for (int i = 0, j = nums.length - 1; i < nums.length; i++) {
            if (nums[i] == 0){
                temp = nums[j];
                nums[j] = nums[i];
                nums[i] = temp;

                j--;
            }
        }


        System.out.println(Arrays.toString(nums));

    }
}
